package com.newly.driver.sdk.device.video.service.impl.tencent;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.newly.driver.sdk.device.video.cloud.stream.QueryLiveStreamReq;
import com.newly.driver.sdk.device.video.cloud.stream.QueryLiveStreamResp;
import com.newly.driver.sdk.device.video.constants.DeviceConstant;
import com.newly.driver.sdk.device.video.exception.NewlyDeviceException;
import com.newly.driver.sdk.device.video.service.api.IStream;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iotvideoindustry.v20201201.models.DescribeDeviceStreamsRequest;
import com.tencentcloudapi.iotvideoindustry.v20201201.models.DescribeDeviceStreamsResponse;

/**
 * @author gjm guan
 * @date 2021/10/21 14:51
 * @desc
 */
public class TencentStream extends AbstractTencentCloud implements IStream {

    @Override
    public QueryLiveStreamResp queryLiveStream(QueryLiveStreamReq queryLiveStreamReq) {
        try {
            Assert.notNull(queryLiveStreamReq, "查询直播流参数异常");
            Assert.notBlank(queryLiveStreamReq.getDeviceId(), "deviceId不能为空");
            Assert.notBlank(queryLiveStreamReq.getChannelId(), "channelId不能为空");
            //Assert.notNull(queryLiveStreamReq.getExpireTime(), "expireTime不能为空");
        } catch (IllegalArgumentException e) {
            throw new NewlyDeviceException(e.getMessage());
        }

        // 设置请求参数
        DescribeDeviceStreamsRequest req = new DescribeDeviceStreamsRequest();
        req.setDeviceId(queryLiveStreamReq.getDeviceId());
        req.setChannelId(queryLiveStreamReq.getChannelId());
        long time = DateUtil.date().getTime() / 1000;
        req.setExpireTime(time + DeviceConstant.STREAM_LIVE_EXPIRE_TIME);

        // 发起查询直播流请求
        DescribeDeviceStreamsResponse resp = null;
        try {
            resp = this.httpClient().DescribeDeviceStreams(req);
        } catch (TencentCloudSDKException e) {
            throw new NewlyDeviceException("查询云设备直播流失败：", e);
        }

        // 解析响应数据
        QueryLiveStreamResp queryLiveStreamResp = new QueryLiveStreamResp(resp);

        return queryLiveStreamResp;
    }
}
